#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
//#define printError printf("%s\n", strerror(errno)), exit(1);
#define printError(s)  perror(s), exit(1);
extern int errno;
int max(int a, int b) {return a>=b ? a : b;}
bool isDirectory(char* path)
{
	struct stat ss;
	stat(path, &ss);
	return S_ISDIR(ss.st_mode) ? true : false;
}

int fd_src, fd_tar;
char src[500], tar[500];//路径
char *buf;
char op[30];
int src_path_len, tar_path_len, buf_len; 
int src_length; 

int main(int argc, char *argv[])
{
	
	if(argc!=3)
		printf("Please input thr right size\n");

	else
	{
		strcat(src, argv[1]);
		strcat(tar, argv[2]);
 
		if(isDirectory(src))//源路径是目录 报错
		{
			printf("ERROR:The source file cannot be a directory！\n");
			exit(-1);
		}
 
		if((fd_src=open(src, O_RDWR))==-1) printError(src);
		if((src_length=lseek(fd_src, 0, SEEK_END))==-1) printError(src);
		lseek(fd_src, 0, SEEK_SET);
		buf = (char*)malloc(sizeof(char)*src_length*10);//动态分配内存，存储源文件内容
		if(read(fd_src, buf, max(0, src_length-1))==-1) printError(src);
		buf[strlen(buf)] = '\n';
 
		if(isDirectory(tar))//目标文件是目录,处理出新的目标路径(默认复制后的文件和源文件同名)
		{
			src_path_len = strlen(src); 
			tar_path_len = strlen(tar);
			int pos = 0;
			for(int i = 0; i < src_path_len; i++) 
				if(src[i]=='/') pos=i;
			if(pos==0) tar[tar_path_len++] = '/';
			for(int i = pos; i < src_path_len; i++) 
				tar[tar_path_len++] = src[i];
		}
		//printf("source path:%s\ntarget path:%s\n", src, tar);
 
		if(open(tar, O_RDWR)==-1)//目标文件不存在，创建该文件
		{
			if((fd_tar=open(tar, O_RDWR|O_CREAT, 0666))==-1) printError(tar);//创建失败
		}
		else 
		{
			printf("Object file already exists, which one do you want to operate, overwrite or append?\n");
			printf("Please input overwrite or append?\n");
			scanf("%s", op);
			while(1)
			{
				if(op[0]=='o') //override
				{
					if((fd_tar=open(tar, O_RDWR|O_TRUNC))==-1) printError(tar);
					break;
				}
				else if(op[0]=='a')//append
				{
					if((fd_tar=open(tar, O_RDWR|O_APPEND))==-1) printError(tar);
					break;
				}
				else printf("Please input overwrite or append?\n");
			}
		}
		if(write(fd_tar, buf, max(0, src_length-1))==-1) printError(tar);//写入文件失败
 
		printf("All done!\n");
 
		close(fd_src);
		close(fd_tar);
		free(buf);
	}
	return 0;
}
